package com.wzt.lianfirecontrol.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.fragment.BarFloorFragment;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 报警复核
 */
public class SelectTitleActivity extends BaseActivity {

    private TextView tv_head_title;
    private ImageView iv_down;

    private List<BannerModel> titleList = new ArrayList<>();
    private List<BaseFragment> fragments = new ArrayList<>();
    private int currentSelectTitle = 0;

    private Dialog selectTitleDialog;

    private boolean isFirstLoad = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_select_title);
        setImmerseLayout(findViewById(R.id.rl_select_title), findViewById(R.id.rl_head_content));

        initData();
        initView();
        PreferencesUtils.putString(this, ConstData.NEEDFUHEREFRESH, "1");
    }

    private void initData() {
        BannerModel bannerModel1 = new BannerModel();
        bannerModel1.setTitle("火警复核单");
        titleList.add(bannerModel1);
        BannerModel bannerModel2 = new BannerModel();
        bannerModel2.setTitle("预警复核单");
        titleList.add(bannerModel2);
        BannerModel bannerModel = new BannerModel();
        bannerModel.setTitle("监管复核单");
        titleList.add(bannerModel);


        BaseFragment hjFragment = new BarFloorFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString(ConstData.BARTYPE, "3");
        bundle1.putString(ConstData.BJPAGETYPE, "1");
        hjFragment.setArguments(bundle1);
        fragments.add(hjFragment);

        BaseFragment yjFragment = new BarFloorFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString(ConstData.BARTYPE, "3");
        bundle2.putString(ConstData.BJPAGETYPE, "2");
        yjFragment.setArguments(bundle2);
        fragments.add(yjFragment);

        BaseFragment jgFragment = new BarFloorFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ConstData.BARTYPE, "3");
        bundle.putString(ConstData.BJPAGETYPE, "0");
        jgFragment.setArguments(bundle);

        fragments.add(jgFragment);
    }

    private void initView() {
        initHeadTitle();
    }

    private void initHeadTitle() {
        tv_head_title = findViewById(R.id.tv_head_title);
        iv_down = findViewById(R.id.iv_down);
        if (fragments.size() == 1){
            iv_down.setVisibility(View.GONE);
        }
        tv_head_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectTitleDialog = DialogUtils.getChooseListDialog(SelectTitleActivity.this, titleList,
                        "", currentSelectTitle, new BaseRecyclerAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View v, int position, Object data) {
                                DialogUtils.closeDialog(selectTitleDialog);
                                currentSelectTitle = position;
                                setTitleAction();
                            }
                        });
                DialogUtils.showDialog(selectTitleDialog);
            }
        });
        iv_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectTitleDialog = DialogUtils.getChooseListDialog(SelectTitleActivity.this, titleList,
                        "", currentSelectTitle, new BaseRecyclerAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View v, int position, Object data) {
                                DialogUtils.closeDialog(selectTitleDialog);
                                currentSelectTitle = position;
                                setTitleAction();
                            }
                        });
                DialogUtils.showDialog(selectTitleDialog);
            }
        });

        setTitleAction();
    }

    private void setTitleAction(){
        tv_head_title.setText(titleList.get(currentSelectTitle).getTitle());
        addFragment(R.id.fl_container, (BaseFragment) fragments.get(currentSelectTitle));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DialogUtils.closeDialog(selectTitleDialog);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirstLoad){
            if ("0".equals(PreferencesUtils.getString(this, ConstData.NEEDFUHEREFRESH, "1"))) {
                PreferencesUtils.putString(this, ConstData.NEEDFUHEREFRESH, "1");
                if (null != fragments && fragments.size() > currentSelectTitle){
                    fragments.get(currentSelectTitle).updateData();
                }
            }
        }
        isFirstLoad = false;

    }
}
